export PROJECT_JUSTFILE := "1" # Note: used in build.py
PG_MAJOR := env("PG_MAJOR", "17")
PG_BIN := env("PG_BIN", "/usr/lib/postgresql/" + PG_MAJOR + "/bin")

# Show list of recipes
default:
    @just --list

ci: docker-build docker-run
  #!/usr/bin/env bash
  set -euo pipefail
  pwd
  trap "python3 build.py docker-stop; python3 build.py docker-rm" EXIT
  docker exec pgai-ext just build
  docker exec pgai-ext just lint
  docker exec pgai-ext just format
  docker exec pgai-ext just install-all
  docker exec -d pgai-ext just test-server
  docker exec pgai-ext just test
  build.py docker-stop
  build.py docker-rm

clean:
	@./build.py clean

clean-sql:
	@./build.py clean-sql

clean-py:
	@./build.py clean-py

build:
	@PG_BIN={{PG_BIN}} ./build.py build

install:
	@PG_BIN={{PG_BIN}} ./build.py install

install-all:
	@PG_BIN={{PG_BIN}} ./build.py install all

build-install:
	@PG_BIN={{PG_BIN}} ./build.py build-install

install-sql:
	@PG_BIN={{PG_BIN}} ./build.py install-sql

install-sql-all:
	@PG_BIN={{PG_BIN}} ./build.py install-sql all

install-prior-py:
	@./build.py install-prior-py

install-py:
	@./build.py install-py

uninstall:
	@PG_BIN={{PG_BIN}} ./build.py uninstall

uninstall-sql:
	@PG_BIN={{PG_BIN}} ./build.py uninstall-sql

uninstall-py:
	@./build.py uninstall-py

freeze:
	@./build.py freeze

build-sql:
	@./build.py build-sql

build-release:
    @./build.py build-release

test-server:
	@./build.py test-server

test:
	@./build.py test

lint-sql:
	@./build.py lint-sql

lint-py:
	@./build.py lint-py

lint:
	@./build.py lint

lint-fix:
    @./build.py lint-fix

format:
	@./build.py format

format-fix:
	@./build.py format-fix

# Run both linter and type-checking checks and fix all auto-fixable issues
fix: lint-fix format-fix

check-requirements:
    @./build.py check-requirements

docker-build:
	@PG_MAJOR={{PG_MAJOR}} ./build.py docker-build

docker-run:
	@./build.py docker-run

docker-start:
	@./build.py docker-start

docker-stop:
	@./build.py docker-stop

docker-rm:
	@./build.py docker-rm

run:
	@PG_MAJOR={{PG_MAJOR}} PG_BIN={{PG_BIN}} ./build.py run
	@docker exec -it -u postgres pgai-ext /bin/bash -c "set -e; if [ -f .env ]; then set -a; source .env; set +a; fi; psql"

# Launches a bash shell in the container
docker-shell:
	@docker exec -it -u root pgai-ext /bin/bash

# Launches a psql shell in the container
psql-shell:
	@docker exec -it -u postgres pgai-ext /bin/bash -c "set -e; if [ -f .env ]; then set -a; source .env; set +a; fi; psql"

# Runs tests that rely on snapshots against PG 17 and 16 and updates the
# expected files with the generated actual files.
update-snapshots: build-sql docker-rm docker-build docker-run
    #!/usr/bin/env bash
    set -uo pipefail
    docker exec pgai-ext just install-py install-sql
    docker exec pgai-ext uv run pytest tests/contents/test_contents.py
    rm -f tests/contents/output17.expected
    mv tests/contents/output17.actual tests/contents/output17.expected
    docker exec pgai-ext uv run pytest tests/privileges/test_privileges.py
    for f in tests/privileges/*.actual; do
        if [ -f "$f" ]; then
            expected="${f%.actual}.expected"
            rm -f "$expected"
            mv "$f" "$expected"
        fi
    done
    just docker-rm
    PG_MAJOR=16 just docker-build docker-run
    docker exec pgai-ext just install-py install-sql
    docker exec pgai-ext uv run pytest tests/contents/test_contents.py
    rm -f tests/contents/output16.expected
    mv tests/contents/output16.actual tests/contents/output16.expected
    just docker-rm
